home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdInputImage.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  6.0 KB  |  205 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Input Tag Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. // dialog initialization code
  38.  
  39. function Startup()
  40. {
  41.   if (!InitEditorShell())
  42.     return;
  43.  
  44.   gDialog = {
  45.     inputName:      document.getElementById( "InputName" ),
  46.     inputDisabled:  document.getElementById( "InputDisabled" ),
  47.     inputTabIndex:  document.getElementById( "InputTabIndex" )
  48.   };
  49.  
  50.   ImageStartup();
  51.  
  52.   // Get a single selected input element
  53.   var tagName = "input";
  54.   imageElement = editorShell.GetSelectedElement(tagName);
  55.  
  56.   if (imageElement)
  57.   {
  58.     // We found an element and don't need to insert one
  59.     gInsertNewImage = false;
  60.   }
  61.   else
  62.   {
  63.     gInsertNewImage = true;
  64.  
  65.     // We don't have an element selected,
  66.     //  so create one with default attributes
  67.  
  68.     imageElement = editorShell.CreateElementWithDefaults(tagName);
  69.     if( !imageElement )
  70.     {
  71.       dump("Failed to get selected element or create a new one!\n");
  72.       window.close();
  73.       return;
  74.     }
  75.  
  76.     var imgElement = editorShell.GetSelectedElement("img");
  77.     if (imgElement)
  78.     {
  79.       // We found an image element, convert it to an input type="image"
  80.       var attributes = ["src", "alt", "width", "height", "hspace", "vspace", "border", "align", "usemap", "ismap"];
  81.       for (i in attributes)
  82.         imageElement.setAttribute(attributes[i], imageElement.getAttribute(attributes[i]));
  83.     }
  84.   }
  85.  
  86.   // Make a copy to use for AdvancedEdit
  87.   globalElement = imageElement.cloneNode(false);
  88.  
  89.   // We only need to test for this once per dialog load
  90.   gHaveDocumentUrl = GetDocumentBaseUrl();
  91.  
  92.   InitDialog();
  93.  
  94.   // Save initial source URL
  95.   gOriginalSrc = gDialog.srcInput.value;
  96.  
  97.   // By default turn constrain on, but both width and height must be in pixels
  98.   gDialog.constrainCheckbox.checked =
  99.     gDialog.widthUnitsMenulist.selectedIndex == 0 &&
  100.     gDialog.heightUnitsMenulist.selectedIndex == 0;
  101.  
  102.   SetTextboxFocus(gDialog.inputName);
  103.  
  104.   SetWindowLocation();
  105. }
  106.  
  107. function InitDialog()
  108. {
  109.   InitImage();
  110.   gDialog.inputName.value = globalElement.getAttribute("name");
  111.   gDialog.inputDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  112.   gDialog.inputTabIndex.value = globalElement.getAttribute("tabindex");
  113. }
  114.  
  115. function ValidateData()
  116. {
  117.   if (!ValidateImage())
  118.     return false;
  119.   if (gDialog.inputName.value)
  120.     globalElement.setAttribute("name", gDialog.inputName.value);
  121.   else
  122.     globalElement.removeAttribute("name");
  123.   if (gDialog.inputTabIndex.value)
  124.     globalElement.setAttribute("tabindex", gDialog.inputTabIndex.value);
  125.   else
  126.     globalElement.removeAttribute("tabindex");
  127.   if (gDialog.inputDisabled.checked)
  128.     globalElement.setAttribute("disabled", "");
  129.   else
  130.     globalElement.removeAttribute("disabled");
  131.   globalElement.setAttribute("type", "image");
  132.   return true;
  133. }
  134.  
  135. function onAccept()
  136. {
  137.   // Show alt text error only once
  138.   // (we don't initialize doAltTextError=true
  139.   //  so Advanced edit button dialog doesn't trigger that error message)
  140.   // Use this now (default = false) so Advanced Edit button dialog doesn't trigger error message
  141.   gDoAltTextError = true;
  142.  
  143.   if (ValidateData())
  144.   {
  145.  
  146.     editorShell.BeginBatchChanges();
  147.  
  148.     if (gRemoveImageMap)
  149.     {
  150.       globalElement.removeAttribute("usemap");
  151.       if (gImageMap)
  152.       {
  153.         editorShell.DeleteElement(gImageMap);
  154.         gInsertNewIMap = true;
  155.         gImageMap = null;
  156.       }
  157.     }
  158.     else if (gImageMap)
  159.     {
  160.       // Assign to map if there is one
  161.       var mapName = gImageMap.getAttribute("name");
  162.       if (mapName != "")
  163.       {
  164.         globalElement.setAttribute("usemap", ("#"+mapName));
  165.         if (globalElement.getAttribute("border") == "")
  166.           globalElement.setAttribute("border", 0);
  167.       }
  168.  
  169.       if (gInsertNewIMap)
  170.       {
  171.         try
  172.         {
  173.           editorShell.editorDocument.body.appendChild(gImageMap);
  174.         //editorShell.InsertElementAtSelection(gImageMap, false);
  175.         }
  176.         catch (e)
  177.         {
  178.           dump("Exception occured in InsertElementAtSelection\n");
  179.         }
  180.       }
  181.     }
  182.  
  183.     editorShell.CloneAttributes(imageElement, globalElement);
  184.  
  185.     if (gInsertNewImage)
  186.     {
  187.       try {
  188.         // 'true' means delete the selection before inserting
  189.         // in case were are converting an image to an input type="image"
  190.         editorShell.InsertElementAtSelection(imageElement, true);
  191.       } catch (e) {
  192.         dump(e);
  193.       }
  194.     }
  195.  
  196.     editorShell.EndBatchChanges();
  197.  
  198.     SaveWindowLocation();
  199.  
  200.     return true;
  201.   }
  202.   return false;
  203. }
  204.  
  205.